home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snip9611.zip / ARCHIVE.CPP < prev    next >
C/C++ Source or Header  |  1996-11-24  |  2KB  |  102 lines

  1. // +++Date last modified: 16-Nov-1996
  2.  
  3. /*
  4. **  ARCHIVE.CPP
  5. **
  6. **   Written by Jari Laaksonen (2:221/360.20), 2 Sep 1994
  7. **   Based on the code by Heinz Ozwirk and David Gersic.
  8. **
  9. **   Free for all participants of the C_ECHO & other conferences where
  10. **   this code is posted. Released to SNIPPETS by the authors.
  11. **
  12. **   See the file WhichArc.DOC about archive types and file offsets
  13. **   for self extracting archives.
  14. */
  15.  
  16. #include "archive.hpp"
  17. #include "whicharc.h"
  18.  
  19. int Archive::Scan (FILE *fp, char *szBuff, long flen)
  20. {
  21.       if (offset > flen)
  22.             return 0;
  23.  
  24.       int rc;
  25.  
  26.       rc = SeekFile (fp, szBuff, offset);
  27.       if (rc > 0)
  28.             rc = Check (szBuff);
  29.  
  30.       return (rc > UNKNOWN ? sfxtype : rc);
  31. }
  32.  
  33. // Generic fingerprint check.
  34. // LHA and PAK/ARC needs more complicated checking
  35. // If some new archiver needs different check algorithm, derive a new
  36. // class from class Archive and write a new Check() function.
  37.  
  38. int Archive::Check (char *szBuff)
  39. {
  40.       char *p;
  41.  
  42.       p = fingerprint;
  43.       while (*p)
  44.       {
  45.             if (*p != *szBuff)
  46.                   return UNKNOWN;
  47.             p++;
  48.             szBuff++;
  49.       }
  50.  
  51.       return type;
  52. }
  53.  
  54. int LhaArchive::Check (char *szBuff)
  55. {
  56.       for (int c = 0, i = szBuff[0]; i--; c += (szBuff + 2)[i])
  57.             ;
  58.       if (
  59.           ((unsigned char) (c & 0x00FF)) == szBuff[1]
  60.           && szBuff[2] == fingerprint[0]
  61.           && szBuff[3] == fingerprint[1]
  62.           && szBuff[4] == fingerprint[2]
  63.           && szBuff[6] == fingerprint[3]
  64.          )
  65.       {
  66.             if (szBuff[5] > '1')
  67.             {
  68.                   sfxtype = SFXLHA;
  69.                   return LHA;
  70.             }
  71.             else
  72.             {
  73.                   sfxtype = SFXLHARC;
  74.                   return LHARC;
  75.             }
  76.       }
  77.       else  return UNKNOWN;
  78. }
  79.  
  80. int PakArchive::Check (char *szBuff)
  81. {
  82.       if (szBuff[0] == 0x1A)
  83.       {
  84.             if (szBuff[1] == 0x0A || szBuff[1] == 0x0B)
  85.             {
  86.                   sfxtype = SFXPAK;
  87.                   return PAK;
  88.             }
  89.             else if (szBuff[1] > 0x14)
  90.             {
  91.                   sfxtype = SFXARC6;
  92.                   return ARC6;
  93.             }
  94.             else
  95.             {
  96.                   sfxtype = SFXARC;
  97.                   return ARC;
  98.             }
  99.       }
  100.       else  return UNKNOWN;
  101. }
  102.